﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Data.Linq;
using NomClasses;

namespace NomCafe
{
    public partial class frmItemTypeManager : DevComponents.DotNetBar.Office2007RibbonForm
    {
        string nameItemType;

        public frmItemTypeManager()
        {
            InitializeComponent();            
        }

        private void frmItemTypeManager_Load(object sender, EventArgs e)
        {
            getItemCategory();
        }

        private void getItemCategory()
        {
            listViewItemCategory.Items.Clear();
            // Lấy item category cho listViewItemCatagory            
            NomClasses.NomDataContext nc = new NomClasses.NomDataContext();
            System.Data.Linq.Table<NomClasses.nom_item_category> Category = nc.nom_item_categories;
            foreach (NomClasses.nom_item_category itemCate in Category)
            {                
                listViewItemCategory.Items.Add(itemCate.name).SubItems.Add(itemCate.note);
            }
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            bool flag = false;
            try
            {
                if (txtNameItem.Text != "")
                {
                    NomDataContext nc = new NomDataContext();
                    var itemCate = from iteCa in nc.nom_item_categories
                                   select iteCa;
                    foreach(var i in itemCate)
                    {
                        if (txtNameItem.Text.Trim().ToLower().Equals(i.name.Trim().ToLower()))
                        {
                            flag = true;
                        }                                                
                    }
                    if (flag == true)
                    {
                        MessageBox.Show(languageManager.ALREADY_ITEMTYPE_ERROR, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        nom_item_category itemCategory = new nom_item_category();
                        itemCategory.name = txtNameItem.Text;
                        itemCategory.note = txtNoteItem.Text;
                        nc.nom_item_categories.InsertOnSubmit(itemCategory);
                        nc.SubmitChanges();

                        listViewItemCategory.Items.Clear();
                        getItemCategory();

                        txtNameItem.Text = "";
                        txtNoteItem.Text = "";
                        txtNameItem.Focus();                        
                    }
                }
                else
                {
                    MessageBox.Show(languageManager.INSERT_ITEMTYPE_ERROR, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void btnRenew_Click(object sender, EventArgs e)
        {
            getItemCategory();

            txtNameItem.Text = "";
            txtNoteItem.Text = "";

            btnDelete.Enabled = false;
            btnSaveItemType.Enabled = false;
            btnAccept.Enabled = true;
            btnRenew.Enabled = true;
        }

        private void listViewItemCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnDelete.Enabled = true;
            btnSaveItemType.Enabled = true;
            btnAccept.Enabled = false;

            nameItemType = "";
            for (int i = 0; i < listViewItemCategory.Items.Count; i++)
            {
                if (listViewItemCategory.Items[i].Selected)
                {
                    nameItemType = listViewItemCategory.Items[i].SubItems[0].Text;
                    txtNameItem.Text = nameItemType;
                    txtNoteItem.Text = listViewItemCategory.Items[i].SubItems[1].Text;
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                NomDataContext nc = new NomDataContext();
                nom_item_category itemCategory = nc.nom_item_categories.Single(i => i.name == nameItemType);
                nc.nom_item_categories.DeleteOnSubmit(itemCategory);
                nc.SubmitChanges();
                
                btnRenew_Click(sender, e);
            }
            catch (Exception)
            {
                MessageBox.Show( languageManager.DELETE_ITEMTYPE_ERROR, "ERROR" , MessageBoxButtons.OK , MessageBoxIcon.Error);
            }
        }

        private void btnSaveItemType_Click(object sender, EventArgs e)
        {
            bool flag = false;
            try
            {
                NomDataContext nc = new NomDataContext();
                var itemCate = from iteCa in nc.nom_item_categories
                               where iteCa.name != nameItemType
                               select iteCa;
                    foreach(var i in itemCate)
                    {
                        if (txtNameItem.Text.Trim().ToLower().Equals(i.name.Trim().ToLower()))
                        {
                            flag = true;
                        }                                                
                    }
                    if (flag == true)
                    {
                        MessageBox.Show(languageManager.ALREADY_ITEMTYPE_ERROR, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        nom_item_category itemCategory = nc.nom_item_categories.Single(i => i.name == nameItemType);
                        itemCategory.name = txtNameItem.Text;
                        itemCategory.note = txtNoteItem.Text;
                        nc.SubmitChanges();

                        btnRenew_Click(sender, e);
                    }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        
    }
}
